package com.hfuu.service.impl;

import com.hfuu.dao.read.custom.TbUserOrginzerReadDao;
import com.hfuu.dao.write.custom.TbUserOrginzerWriteDao;
import com.hfuu.enums.system.ExceptionEnum;
import com.hfuu.exceptions.ServiceExceptionSpec;
import com.hfuu.model.po.TbUserOrginzer;
import com.hfuu.model.po.TbUserOrginzerExample;
import com.hfuu.service.schedule.ScheduleService;
import com.hfuu.service.schedule.request.UserScheduleRequest;
import com.hfuu.service.user.OrginzerService;
import com.hfuu.service.user.request.OrginzerRequest;
import com.hfuu.service.user.response.OrginzerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author: admin
 * Date: 2018/4/21
 * time: 上午10:48
 */

@Component
@Slf4j
public class OrginzerServiceImpl implements OrginzerService{

    @Autowired
    private TbUserOrginzerReadDao tbUserOrginzerReadDao;

    @Autowired
    private TbUserOrginzerWriteDao tbUserOrginzerWriteDao;

    @Autowired
    private ScheduleService scheduleService;
    /**
     * 查询我的组织列表
     * @param request joinStatus 如果代表已参加，传的是用户的Id
     * @return
     * @throws ServiceExceptionSpec
     */
    @Override
    public List<OrginzerResponse> listMyOrgizer(OrginzerRequest request) throws ServiceExceptionSpec {

        return tbUserOrginzerReadDao.listMyOrginzer(request.getUserId(), request.getOrginzerName(), request.getJoinStatus());
    }

    /**
     * 参加组织
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer joinOrginzer(OrginzerRequest request) throws Exception {

        if (request.getOrginzerId() == null ||
                request.getUserId() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }
        TbUserOrginzer tbUserOrginzer = new TbUserOrginzer();
        tbUserOrginzer.setOrginzerId(request.getOrginzerId());
        tbUserOrginzer.setUserId(request.getUserId());
        tbUserOrginzerWriteDao.insertSelective(tbUserOrginzer);

        UserScheduleRequest userScheduleRequest = new UserScheduleRequest();
        userScheduleRequest.setUserId(request.getUserId());
        scheduleService.synchronizeScheduleById(userScheduleRequest);

        return 0;
    }

    /**
     * 退出所参加的组织
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public Integer outOrginzer(OrginzerRequest request) throws Exception {
        if (request.getOrginzerId() == null ||
                request.getUserId() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }
        TbUserOrginzer tbUserOrginzer = new TbUserOrginzer();
        TbUserOrginzerExample example = new TbUserOrginzerExample();
        TbUserOrginzerExample.Criteria criteria = example.createCriteria();
        criteria.andDisabledEqualTo(0).andOrginzerIdEqualTo(request.getOrginzerId())
                .andUserIdEqualTo(request.getUserId());
        tbUserOrginzer.setDisabled(1);
        tbUserOrginzerWriteDao.updateByExampleSelective(tbUserOrginzer, example);

        UserScheduleRequest userScheduleRequest = new UserScheduleRequest();
        userScheduleRequest.setUserId(request.getUserId());
        userScheduleRequest.setOrginzerId(request.getOrginzerId());
        scheduleService.deleteScheduleById(userScheduleRequest);

        return 0;
    }
}
